package net.brucecooper.mindmap.client;

import gwt.canvas.client.Canvas;

public abstract class Panel extends GraphicComponent {
	enum Alignment {
	    LEFT, CENTER, RIGHT, TOP, BOTTOM
	}
	
    protected GraphicComponent[] children = null;
    protected int childCount;
    protected Alignment alignment;
    protected int spacing;

	public Panel(Alignment alignment) {
        this.alignment = alignment;
    }

    public Panel() {
        this(Alignment.CENTER);
    }

    
    @Override
    public GraphicComponent getWidget(int index) {
        return children == null ? null : children[index];
    }

    @Override
    public int getWidgetCount() {
        return childCount;
    }

    @Override
    public void add(int index, GraphicComponent comp) {
        if (children == null) {
            children = new GraphicComponent[5];
        }
        if (index >= children.length) {
            GraphicComponent[] newKids = new GraphicComponent[index + 5];
            System.arraycopy(children, 0, newKids, 0, children.length);
            children = newKids;
        }
        children[index] = comp;
        childCount++;
        comp.parent = this;
    }

    @Override
    public void add(GraphicComponent comp) {
        this.add(childCount, comp);
    }

    @Override
    public boolean remove(GraphicComponent comp) {
        int index = getWidgetIndex(comp);
        if (index == -1)
            return false;
        else {
            remove(index);
            return true;
        }
    }

    @Override
    public void remove(int index) {
        if (children != null) {
            children[index].parent = null;
            System.arraycopy(children, index+1, children, index, childCount-index-1);
            childCount--;
        }
    }

    @Override
    public int getWidgetIndex(GraphicComponent comp) {
        if (children != null) {
            for (int i = 0; i < childCount; i++) {
                if (children[i] == comp)
                    return i;
            }
        }
        return -1;
    }

    @Override
    public void paint(Canvas canvas, Clip clip) {
        if (children != null) {
            for (int i = 0; i < childCount; i++) {
                GraphicComponent comp = children[i];
                
                if (clip.intersects(comp.x, comp.y, comp.width, comp.height)) {
                    canvas.save();
                    canvas.translate(comp.x, comp.y);
                    // canvas.strokeRect(0,0,comp.width,comp.height);
                    comp.paint(canvas, clip.translate(comp.x, comp.y));
                    canvas.restore();
                }
            }
        }
        paintComponent(canvas, clip);
    }

    

    @Override
    public GraphicComponent findDeepest(int x, int y) {
        if (children != null) {
            for (int i = 0; i < childCount; i++) {
                GraphicComponent child = children[i];
                if (x >= child.x && y >= child.y && x < child.x + child.width && y < child.y + child.height) {
                    return child.findDeepest(x - child.x, y - child.y);

                }
            }
        }
        return this;
    }

    public int getSpacing() {
		return spacing;
	}

	public void setSpacing(int spacing) {
		this.spacing = spacing;
	}


}

